# -*- coding:utf-8 -*-
import sys
import json
from decimal import Decimal
import MySQLdb
import datetime
def default(obj):
    if isinstance(obj, Decimal):
        return str(obj)
    raise TypeError
#连接数据库
dbstr=sys.argv[1]
city=sys.argv[2]
print dbstr
print city
d1 = datetime.datetime.now()
#conn=MySQLdb.connect(host='rds1tu038044m8zub4o7.mysql.rds.aliyuncs.com',user='zhuge',passwd='zhuge1116',db=dbstr,port=3306,charset="utf8")
conn=MySQLdb.connect(host='101.200.81.152',user='zhugef_online',passwd='emd3ZW56*$FuYmFanVuZmVuZw%db',db=dbstr,port=3307,charset="utf8")
conn_off=MySQLdb.connect(host='182.92.96.120',user='zhugef_offline',passwd='2d573c729a&^%460adc4c5547ff7e7%8',db=dbstr,port=3306,charset="utf8")
cur = conn.cursor()
cur_off = conn_off.cursor()
cur.execute('select count(*) from house_sell_gov')
cur_off.execute('select count(*) from house_sell_gov_new')
#获取数据库中记录总条数
count_gov_online=cur.fetchone()[0]
print "count_gov_online=",count_gov_online
count_gov_offline=cur_off.fetchone()[0]
print "count_gov_offline=",count_gov_offline
cur_off.execute('select count(*) from house_sell_info_new')
count_info_offline=cur_off.fetchone()[0]
print "count_info_offline=",count_info_offline
cur_off.execute('select count(*) from house_sell_new')
count_sell_offline=cur_off.fetchone()[0]
print "count_sell_offline=",count_sell_offline
cur_off.execute('select sum(cnt) from house_sell_new')
sum_cnt=cur_off.fetchone()[0]
print "sum_cnt=",sum_cnt

if ((count_gov_online != count_gov_offline) or (count_gov_offline != count_info_offline)):
    print "count not match"
    #sys.exit(1)
if (count_sell_offline > count_info_offline):
    print "sell bigger than info"
    #sys.exit(1)
if (sum_cnt <= count_info_offline):
    print "sum cnt less then info"
merge_rate=(count_gov_offline-count_sell_offline)*100/count_gov_offline
print "merge_rate=", merge_rate
if ((merge_rate > 20) or (merge_rate < 2)):
    print "merge rate too low or too high"
    #sys.exit(1)
cur_off = conn_off.cursor(cursorclass=MySQLdb.cursors.DictCursor)
sql="SELECT CONCAT(house_sell_new.id,','),CONCAT(house_sell_info_new.id,',') FROM house_sell_new LEFT JOIN  house_sell_info_new ON house_sell_new.id=house_sell_info_new.id WHERE house_sell_info_new.id is null"
cur_off.execute(sql)
results=cur.fetchall()
print results
if results != ():
    print "sell sell_info correlation not match"
    #sys.exit(1)
